package com.example.service.wechat.impl;

import com.example.pojo.web.City;
import com.example.pojo.web.Stop;
import com.example.pojo.wechat.WxCity;
import com.example.pojo.wechat.WxPoint;
import com.example.pojo.wechat.WxStop;
import com.example.service.web.CityService;
import com.example.service.wechat.WxCityService;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName WxCityServiceImpl
 * @Description: com.example.service.wechat.impl
 * @Auther: xiwd
 * @Date: 2022/4/16 - 04 - 16 - 21:46
 * @version: 1.0
 */
@Service
public class WxCityServiceImpl implements WxCityService {
    //properties
    @Autowired
    private CityService cityService;

    //methods
    @Override
    public List<WxCity> selectAllCityWithStops() {
        try {
            List<City> cities = cityService.selectAllCityWithStops();
            if (cities==null)
                return null;
            List<WxCity> cityList = new ArrayList<>();
            cities.forEach(e->{
                WxCity wxCity = new WxCity();
                Coordinate coordinate = e.getCenterpoint().getCoordinate();
                wxCity.setCityid(e.getCityid());
                wxCity.setCityname(e.getCityname());
                wxCity.setCenterpoint(new WxPoint(coordinate.getY(),coordinate.getX()));
                //添加停车区域
                List<WxStop> wxStops = new ArrayList<>();
                List<Stop> stops = e.getStops();
                if (stops!=null&&stops.size()!=0){
                    stops.forEach(ele->{
                        Polygon geom = ele.getGeom();
                        Coordinate[] coordinates = geom.getCoordinates();
                        List<WxPoint> boundary = new ArrayList<>();
                        for (int i = 0; i < coordinates.length; i++) {
                            boundary.add(new WxPoint(coordinates[i].getY(),coordinates[i].getX()));
                        }
                        Point centroid = geom.getCentroid();
                        WxStop wxStop = new WxStop();
                        wxStop.setId(ele.getId());
                        wxStop.setStopname(ele.getStopname());
                        wxStop.setSize(ele.getSize());
                        wxStop.setStopno(ele.getStopno());
                        wxStop.setCenterpoint(new WxPoint(centroid.getY(),centroid.getX()));
                        wxStop.setBoundary(boundary);
                        //添加停车区
                        wxStops.add(wxStop);
                    });
                    //添加停车区列表
                    wxCity.setStops(wxStops);
                }else {
                    wxCity.setStops(wxStops);
                }
                //添加城市
                cityList.add(wxCity);
            });
            return cityList;
       }catch (Exception e){
           e.printStackTrace();
           return null;
       }
    }
}
